import {productCategorize,findCommodity} from '../../../server/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    currentTab:0,
    typelist:[],
    id:'',
    list:[],
    itemValue:'',
    commodityName:'',
    pageNo:1,
    pageSize:10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
        console.log(options,'opp');
        const {id} = options
        this.setData({id:id})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  switchNav: function (e) {
    this.setData({
      list:[],
      pageNo:1,
      pageSize:10,
      commodityName:'',
      
    })
  
    var page = this;
    var id = e.target.id;
    const val = e.currentTarget.dataset.value
    page.setData({itemValue:val})
    this.getdata()
    if (this.data.currentTab == id) {
      return false;
    } else {
      page.setData({
        currentTab: id
      });
    }
    page.setData({
      active: id
    });
  
  },
  getMore(){
    this.data.pageNo++ 
    const params = {
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize
    }
    this.getdata(params)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list:[],
      pageNo:1,
      pageSize:10
    })
      this.typedata()
      this.getdata()
    
  },
  //获取商品
  async getdata(val){
    console.log(typeof this.data.itemValue);
        const params = {
          pageSize:val == undefined ? this.data.pageSize : val.pageSize,
          pageNo:val == undefined ? this.data.pageNo : val.pageNo,
          typeName:this.data.itemValue,
          commodityName:this.data.commodityName,
          businessId:this.data.id
        }
        const data = await findCommodity(params)
        if(data.code == 200)
        {
          const source =  data.result.records
          this.setData({list:val == undefined  ? source : [...this.data.list,...source]})
        }
  },
  //获取商品类型
   async typedata(){
      const data = await productCategorize()
      if(data.code == 200)
      {
        const val = data.result
          val.unshift({itemText:'全部',itemValue:''})
        console.log(val,'arr');
        this.setData({typelist:val})
      }
  },
  detaillist(e){
    const spid = e.currentTarget.dataset.id
    console.log(spid,'spid');
     wx.navigateTo({
            url: `/subPackagesA/pages/productdetail/productdetail?id=${spid}`,
          });
  },
  //搜索
  onSearch(){
    this.getdata()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})